package cn.edu.neusoft.wuguangsheng.market.activity;

import android.content.Intent;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.adapter.CartFoodsAdapter;
import cn.edu.neusoft.wuguangsheng.market.bean.CartFoodsBean;
import cn.edu.neusoft.wuguangsheng.market.bean.CartPriceBean;
import cn.edu.neusoft.wuguangsheng.market.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.CartFoodsModel;
import cn.edu.neusoft.wuguangsheng.market.model.CartPriceModel;

public class CartFoodsActivity extends BaseActivity{
    RecyclerView recycleallfoods;
    private List<CartFoodsBean> items;
    String mrid;
    TextView cartfoodsprice;
    Boolean up=false;//默认false不刷新
    Button cart_btn_buy;

    //使用handler方法
    public android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    CartPriceModel cartPriceModel=new CartPriceModel();
                    cartPriceModel.showCartPrice(getid(),mrid,tListener);
                    //showToast("handler");
                    break;
            }
        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_cart_foods);
         mrid=getIntent().getStringExtra("mrid");
        recycleallfoods=(RecyclerView)findViewById(R.id.recycleallfoods);
        cartfoodsprice=(TextView)findViewById(R.id.cartfoodsprice);
        cart_btn_buy=(Button)findViewById(R.id.cart_btn_buy);




        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(CartFoodsActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleallfoods.setLayoutManager(linearLayoutManager);

        CartFoodsModel cartFoodsModel=new CartFoodsModel();
        String uid=getid();
        cartFoodsModel.showCartFoods(uid,mrid,listListener);
        CartPriceModel cartPriceModel=new CartPriceModel();
        cartPriceModel.showCartPrice(uid,mrid,tListener);
    }

    @Override
    void initEvent() {
        cart_btn_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(CartFoodsActivity.this,CartBuyActivity.class);
                intent.putExtra("mrid",mrid);
                startActivity(intent);
            }
        });
    }

    @Override
    void initDate() {

    }
    ListListener<CartFoodsBean> listListener=new ListListener<CartFoodsBean>() {
        @Override
        public void onResponse(List<CartFoodsBean> list) {
            items=list;
           CartFoodsAdapter cartFoodsAdapter=new CartFoodsAdapter(CartFoodsActivity.this,items,R.layout.newcartcard,handler);
           recycleallfoods.setAdapter(cartFoodsAdapter);
           recycleallfoods.setItemAnimator(new DefaultItemAnimator());
        }
        @Override
        public void onFail(String msg) {

        }
    };
    TListener<CartPriceBean> tListener=new TListener<CartPriceBean>() {
        @Override
        public void onResponse(CartPriceBean cartPriceBean) {
            double price=cartPriceBean.getAllprice();
            String p=String.valueOf(price);
            cartfoodsprice.setText(p);
        }

        @Override
        public void onFail(String msg) {
        }
    };


    //onPause()是失去焦点，不可操作，也是不可见的时候
    @Override
    public void onPause() {
        super.onPause();
        up=true;//不可见的时候将刷新开启

    }
    //onResume()方法，当页面在前台可操作时候会执行这个方法，刷新操作可以放在这个地方
    @Override
    public void onResume() {
        super.onResume();
        if(up){//判断是否刷新
            initView();//向服务器发送请求
            up=false;//刷新一次即可，不需要一直刷新
        }
    }
}
